/**
 * Created by Administrator on 2020/4/18.
 * 消息提示自定义插件
 */

var dialog = {
    confirm:function (msg, callback) {
        layer.confirm(msg, {btn: ['确定', '取消'], title: "提示", success: function (layero, index) {
            layui.jquery(document).on('keydown', function(e){
                if(e.keyCode == 13){
                    layui.layer.close(index);
                    typeof callback === 'function' && callback.call(this);
                    layui.jquery(document).off('keydown');
                    return;
                }
            })
        }}, function (index, layero) {
            layui.layer.close(index);
            typeof callback === 'function' && callback.call();
        });
    },
    post: function (url, data, success_callback, error_callback) {
        layui.jquery.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: data,
            contentType: 'application/json',
            headers: {'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('userdetail')).token},
            success: function (data) {
                if(success_callback == null && data.code == '0') {
                    layer.msg('操作成功', {
                        icon: 6,//成功的表情
                        time: 3000 //3秒关闭（如果不配置，默认是3秒）
                    }, function () {    closeIframe();  });
                    return false;
                } else if(success_callback == null && data.code == '0') {
                    layer.msg('操作失败', {
                        icon: 4,
                        time: 3000
                    }, function () {    closeIframe();  });
                    return false;
                }
                console.log('post:',data);
                typeof success_callback === 'function' && success_callback.call(this, data);
            },
            error: function (data) {
                if(error_callback == null) {
                    layer.msg('操作失败', {
                        icon: 4,//失败的表情
                        time: 3000 //3秒关闭（如果不配置，默认是3秒）
                    }, function () {    closeIframe();  });
                    return false;
                }
                typeof error_callback === 'function' && error_callback.call(this, data);
            }
        });
    },
    postForm: function (url, data, success_callback, error_callback) {
        layui.jquery.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: data,
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            headers: {'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('userdetail')).token},
            success: function (data) {
                if(success_callback == null && data.code == '0') {
                    layer.msg('操作成功', {
                        icon: 6,//成功的表情
                        time: 3000 //3秒关闭（如果不配置，默认是3秒）
                    }, function () {    closeIframe();  });
                    return false;
                } else if(success_callback == null && data.code != '0') {
                    layer.msg('操作失败', {
                        icon: 4,
                        time: 3000
                    }, function () {    closeIframe();  });
                    return false;
                }
                console.log('post:',data);
                typeof success_callback === 'function' && success_callback.call(this, data);
            },
            error: function (data) {
                console.log('error:', JSON.stringify(data));
                if(error_callback == null) {
                    layer.msg('操作失败', {
                        icon: 4,//失败的表情
                        time: 3000 //3秒关闭（如果不配置，默认是3秒）
                    }, function () {    closeIframe();  });
                    return false;
                }
                typeof error_callback === 'function' && error_callback.call(this, data);
            }
        });
    },
    get: function (url, data, success_callback, error_callback) {
        layui.jquery.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('userdetail')).token},
            success: function (data) {
                typeof success_callback === 'function' && success_callback.call(this, data);
            },
            error: function (data) {
                typeof error_callback === 'function' && error_callback.call(this, data);
            }
        });
    },
    iframe: function (title, content, checked, success_callback, end_callback) {
        layui.use(['layer'], function () {
            var layer = layui.layer;
            var index_ = layer.open({
                title: title || '编辑',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: content,
                success: function(layero, index) {
                    if(success_callback == null) {
                        var body = layer.getChildFrame('body', index_);
                        for(var key in checked) {
                            body.find("input[name='"+key+"']").val(checked[key]);
                        }
                        return false;
                    }
                    typeof success_callback === 'function' && success_callback.call(this, index);
                },
                end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
                    typeof end_callback === 'function' && end_callback.call();
                }
            });
            $(window).on("resize", function () {
                layer.full(index_);
            });
        });

    },
    loading: function (callback) {
        layui.use('layer', function () {
            layui.layer.load(1, {shade: [0.1, '#fff'], end: function () {
                typeof callback === 'function' && callback.call();
            }});
        });
    },
    tips: function (content, time, callback) {
        layui.use('layer', function () {
            layui.layer.msg(content, {time: (time || 3) * 1000, shadeClose: true}, function () {
                typeof callback === 'function' && callback.call();
            });
        });
    },
    success: function (content, time, callback) {
        layui.use('layer', function () {
            layui.layer.msg(content, {time: (time || 3) * 1000, shadeClose: true, icon: 1}, function () {
                typeof callback === 'function' && callback.call();
            });
        });
    },
    error: function (content, time, callback) {
        layui.use('layer', function () {
            layui.layer.msg(content, {time: (time || 3) * 1000, shadeClose: true, icon: 2}, function () {
                typeof callback === 'function' && callback.call();
            });
        });
    }
};

function closeIframe() {
    try {
        var iframeIndex = parent.layer.getFrameIndex(window.name);
        parent.layer.close(iframeIndex);
    } catch (e) {}
}